/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.uml2.uml.PseudostateKind;
import org.eclipse.uml2.uml.StateMachine;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * Dialog to select a Connection point for a ConnectionPointReference.
 * 
 * @author David Servat
 * @since 5. apr. 2007
 */
public class ConnectionPointReferencePointTreeSelectionDialog extends Dialog{

	/**
	 * The StateMachine for which we want to show a list of entry/exit points.
	 */
	protected StateMachine stateMachine;

	/**
	 * Chosen pseudostate.
	 */
	protected Pseudostate pseudostate;

	/**
	 * PseudostateKind.
	 */
	protected PseudostateKind kind;

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	/**
	 * Label for description
	 */
	protected Label label;

	/**
	 * Constructor with parent shell and stateMachine.
	 * 
	 * @param parent the Shell.
	 * @param stateMachine the StateMachine.
	 */
	public ConnectionPointReferencePointTreeSelectionDialog(Shell parent, StateMachine stateMachine, PseudostateKind kind){
		super(parent);
		// add the resize ability to the window
		setShellStyle(SWT.RESIZE | super.getShellStyle());
		this.stateMachine = stateMachine;
		this.kind = kind;
	}

	/**
	 * Creates the dialog area.
	 * 
	 * @param parent composite.
	 * 
	 * @return control Control.
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite)super.createDialogArea(parent);

		label = new Label(composite, SWT.NONE);
		label.setText("Select an existing "+kind.getLiteral()+":");
		label.setLayoutData(new GridData());

		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);

		final StateMachine currentSM = stateMachine;

		//build subtree for entry/exit points contained in the StateMachine
		Iterator iter = currentSM.getConnectionPoints().iterator();
		while(iter.hasNext()){
			Pseudostate pseudo = (Pseudostate)iter.next();
			if(pseudo.getKind().equals(kind)){
				TreeItem item = new TreeItem(tree, SWT.SINGLE);
				item.setText(pseudo.getName());
				item.setData(pseudo);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(Pseudostate.class, PseudostateKind.ENTRY_POINT));
			}
		}
		tree.setLayoutData(new GridData(550, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
				if(selection[0].getData() instanceof Pseudostate){
					pseudostate = (Pseudostate)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		return composite;
	}

	/**
	 * Returns the associated Pseudostate.
	 * 
	 * @return pseudostate Pseudostate
	 */
	public Pseudostate getResult(){
		return pseudostate;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(stateMachine.getName()+": "+kind.getLiteral()+" Selection");
	}

}
